
var imgAct=new Image();
var ctx;
var screenWidth;
var screenHeight;
var speed=2;
var horizontalSpeed=speed;
var verticalSpeed=-speed;
var imgActAngle=2;
var star0=new Image();
var gameloopId;
var checkPrize;

function objGmae(){
    this.x=0;
    this.y=0;
    this.image=null;
}
function objAct(){};
objAct.prototype=new objGmae();
objAct.prototype.angle=0;
var objAct=new objAct();

var arrStar=new Array();
function objStar(){};
objStar.prototype=new objGmae();
objStar.prototype.row=0;
objStar.prototype.col=0;
objStar.prototype.hit=false;
var ballgame={
    HasActHitEdge:function(){
        if(objAct.x>screenWidth-objAct.image.width){
            if(horizontalSpeed>0)
            horizontalSpeed=-horizontalSpeed;
        }
        if(objAct<-10){
            if(horizontalSpeed<0)
            horizontalSpeed=-horizontalSpeed;
        }
        if(objAct.y==screenHeight-objAct.image.height){
            verticalSpeed=-speed;
        }
        if(objAct.y<0){
            verticalSpeed=-verticalSpeed;
        }
    },
    chkHitEach:function(obj1,obj2,overlap){
        A1=obj1.x+overlap;
        B1=obj1.x+obj1.image.width-overlap;
        C1=obj1.y+overlap;
        D1=obj1.y+obj1.image.height-overlap;
        A2=obj2.x+overlap;
        B2=obj2.x+obj1.image.width-overlap;
        C2=obj2.y+overlap;
        D2=obj2.y+obj1.image.height-overlap;
        if(A1>A2&&A1<B2||B1>A2&&B1<B2){
            if(C1>C2&&C1<D2||D1>C2&&D1<D2){
                return true;
            }
        }
        return false;
    },
    InitStars:function(){
        var count=0;
        var x=0;
        for(var y=0;y<25;y++){
            star=new objStar();
            star.image=star0;
            star.row=x;
            star.col=y;
            star.x=18*star.col+10;
            star.y=18*star.row+10;
            arrStar[count]=star;
            count++;
        }
    },
    DrawStars:function(){
        for(var x=0;x<arrStar.length;x++){
            var curStar=arrStar[x];
            if(!curStar.hit){
                ctx.drawImage(curStar.image,arrStar[x],arrStar[x].x,arrStar[x].y);
            }
        }
        if(ballgame.AllPrizesHit()){
            clearInterval(gameloopId);
        }
    },
    HasActHitStar:function(){
        for(var x=0;x<arrStar.length;x++){
            var star=arrStar[x];
            if(!star.hit){
                if(ballgame.chkHitEach(star,objAct,0)){
                    star.hit=true;
                    verticalSpeed=speed;
                }
            }
        }
    },
    AllPrizeHit:function(){
        for(var c=0;c<arrStar.length;c++){
            checkPrize=arrStar[c];
            if(checkPrize.hit==false)
             return false;
        }
        return true;
    },
    LoadImages:function(){
        imgAct.src="video/act.png";
        star0.src="video/star.png";
        objAct.image=imgAct;
    }
};
$(function(){
    ballgame.LoadImages();
    ctx=document.getElementsById("cnMain").getContext("2d");
    screenWidth=parseInt(screenWidth/2);
    screenHeight=parseInt(screenHeight/2);
    objAct.x=parseInt(screenWidth/2);
    objAct.y=parseInt(screenHeight/2);
    ballgame.InitStars();
    gameloopId=setInterval(function(){
        ctx.clearRect(0,0,screenWidth,screenHeight);
        ctx.save();
        ballgame.DrawStars;
        objAct.x+=horizontalSpeed;
        objAct.y+=verticalSpeed;
        objAct.angle+=imgActAngle;
        ctx.translate(objAct.x+(objAct.image.width/2),objAct.y+(objAct.image.hieght/2));
        ctx.rotate(objAct.angle*Math.PI/90);
        ctx.drawImage(objAct.image,-(objAct.image.width/2),-(objAct.image.height/2));
        ctx.restore();
        ballgame.HasActHitEdge();
        ballgame.HasActHitStar();
    },10);
});